package com.winhxd.b2c.admin.module.customer.controller;

import com.winhxd.b2c.admin.common.context.UserManager;
import com.winhxd.b2c.admin.common.security.annotation.CheckPermission;
import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.customer.condition.ShoppingMallRuleCondition;
import com.winhxd.b2c.common.domain.customer.vo.CustomerOperationHomePageVO;
import com.winhxd.b2c.common.domain.customer.vo.ShoppingMallRuleVO;
import com.winhxd.b2c.common.domain.order.condition.OrderSalesSummaryCondition;
import com.winhxd.b2c.common.domain.order.vo.OrderBusinessSummaryVO;
import com.winhxd.b2c.common.domain.system.security.enums.PermissionEnum;
import com.winhxd.b2c.common.domain.system.user.vo.UserInfo;
import com.winhxd.b2c.common.feign.customer.CustomerServiceClient;
import com.winhxd.b2c.common.feign.customer.ShoppingMallRuleServiceClient;
import com.winhxd.b2c.common.feign.order.OrderDataStatisticsServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author lishaoping
 * @Description: 商城营业设置
 * @date 2019年1月7日15:07:31
 */
@Api(value = "商城营业设置接口", tags = "商城营业设置接口")
@RestController
@RequestMapping(value = "customer/shoppingMallRule")
public class ShoppingMallRuleController {

    private Logger logger = LoggerFactory.getLogger(ShoppingMallRuleController.class);

    @Autowired
    private ShoppingMallRuleServiceClient shoppingClient;

    @Autowired
    private CustomerServiceClient customerServiceClient;

    @Autowired
    private OrderDataStatisticsServiceClient orderDataStatisticsServiceClient;

    @CheckPermission(PermissionEnum.OPERATION_BACKGROUND_HOME_PAGE)
    @ApiOperation(value = "运营后台首页展示", notes = "运营后台首页展示")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    @PostMapping(value = "/findOperationHomePage")
    public ResponseResult<CustomerOperationHomePageVO> findOperationHomePage() {
        UserInfo currentUser = UserManager.getCurrentUser();
        ResponseResult<CustomerOperationHomePageVO> result = new ResponseResult<CustomerOperationHomePageVO>();
        CustomerOperationHomePageVO vo = new CustomerOperationHomePageVO();
        ResponseResult<CustomerOperationHomePageVO> responseResult = customerServiceClient.findOperationHomePage();
        CustomerOperationHomePageVO v = responseResult.getData();
        vo.setTodayNewMembers(v.getTodayNewMembers());
        vo.setTodayNewManager(v.getTodayNewManager());
        //调用order服务取数据
        OrderSalesSummaryCondition condition = new OrderSalesSummaryCondition();
        condition.setCustomerId(currentUser.getId());
        ResponseResult<OrderBusinessSummaryVO> orderResult = orderDataStatisticsServiceClient.getBusinessSummarySummary(condition);
        OrderBusinessSummaryVO orderVo =  orderResult.getData();
        vo.setOrderServiceNum(orderVo.getOrderServiceNum());
        vo.setOrderNumsToday(orderVo.getOrderNumsToday());
        vo.setSalesToday(orderVo.getSalesToday());
        vo.setOrderSendOutNum(orderVo.getOrderSendOutNum());
        //调用其他服务取数据
        ResponseResult<Integer> withdrawalApplicationResult = customerServiceClient.getWithdrawDepositNum();
        vo.setWithdrawalApplicationCount(withdrawalApplicationResult.getData() == null ? 0 : withdrawalApplicationResult.getData());
        vo.setCurrentTime(new Date());
        result.setData(vo);
        return result;
    }

    @CheckPermission(PermissionEnum.MALL_BUSINESS_SETTING_EDIT)
    @ApiOperation(value = "商城营业设置保存接口", response = Integer.class, notes = "商城营业设置保存接口")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    @PostMapping(value = "/saveShoppingMallRule")
    public ResponseResult<Boolean> saveShoppingMallRule(@RequestBody ShoppingMallRuleCondition condition) {
        UserInfo currentUser = UserManager.getCurrentUser();
        condition.setSysUserId(currentUser.getId());
        ResponseResult<Boolean> result = shoppingClient.saveShoppingMallRule(condition);
        return result;
    }

    @CheckPermission(PermissionEnum.MALL_BUSINESS_SETTING_INFO)
    @ApiOperation(value = "商城营业设置查看接口", response = ShoppingMallRuleVO.class, notes = "商城营业设置查看接口")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    @PostMapping(value = "/findShoppingMallRule")
    public ResponseResult<ShoppingMallRuleVO> findShoppingMallRule() {
        UserInfo currentUser = UserManager.getCurrentUser();
        ShoppingMallRuleCondition condition = new ShoppingMallRuleCondition();
        condition.setSysUserId(currentUser.getId());
        ResponseResult<ShoppingMallRuleVO> result = shoppingClient.findShoppingMallRule(condition);
        return result;
    }
}
